#!/bin/sh
status1=`hdfs haadmin -getServiceState nn1`
status2=`hdfs haadmin -getServiceState nn2`
## 检测namenode是否挂了
name_node_pid=$(ps aux | grep -v grep | grep -i java |grep -i namenode | awk '{print $2}')
if [[ "X${name_node_pid}" != "X" ]] && [[ ${name_node_pid} -gt 0 ]] && kill -0 ${name_node_pid} > /dev/null 2>&1; then
    echo "[$(date +'%Y-%m-%d %H:%M:%S.%N')] namenode is running as process ${name_node_pid}."
    if [[ "active" = "${status1}" ]];then
        echo "current namenode is active"
    else
        if [[ "active" = "${status2}" ]]; then
            echo "current namenode is standby"
            curl "http://172.16.18.53:18080/smartop-frontstage/alarm/smsDiexin.action?phones=15882439584,15378179615&msg=$(hostname -i)%20($(hostname))%20namenode%20failover2standby!!"
            hdfs haadmin -failover nn2 nn1
        fi
    fi
else
    echo "namenode is shutdown"
    /data/hadoop/hadoop-2.3.0-cdh5.1.2/sbin/hadoop-daemon.sh start namenode
    curl "http://172.16.18.53:18080/smartop-frontstage/alarm/smsDiexin.action?phones=15882439584&msg=$(hostname -i)%20($(hostname))%20namenode%20failover2standby!!"
fi